<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;">
      <el-breadcrumb-item :to="{path: '/sys-admin'}">
        <i class="el-icon-s-promotion"></i> 后台管理
      </el-breadcrumb-item>
      <el-breadcrumb-item>员工列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-divider></el-divider>

    <el-table :data="tableData" border style="width: 100%">
<!--      <el-table-column prop="id" label="ID" width="120"></el-table-column>-->
      <el-table-column prop="name" label="员工姓名" width="150"></el-table-column>
<!--      <el-table-column prop="password" label="员工密码" width="150"></el-table-column>-->
      <el-table-column prop="server_id" label="所属部门" width="100"></el-table-column>
      <el-table-column prop="phonenumber" label="联系电话" width="150"></el-table-column>
      <el-table-column prop="level" label="员工职位" width="150"></el-table-column>
      <el-table-column prop="enable" label="启用状态" width="80"></el-table-column>
      <el-table-column prop="uptime" label="员工登录时间" width="230"></el-table-column>
      <el-table-column label="是否启用" align="center" width="80">
        <template slot-scope="scope">
          <el-switch
              v-loading.fullscreen.lock="fullscreenLoading"
              @change="changeEnable(scope.row)"
              v-model="scope.row.enable"
              :active-value="1"
              :inactive-value="0"
              active-color="#13ce66"
              inactive-color="#ccc">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="100">
        <template slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit" size="mini" circle
                     @click="handleEdit(scope.row)"></el-button>
          <el-button type="danger" icon="el-icon-delete" size="mini" circle
                     @click="openDeleteConfirn(scope.row)"
                     v-loading.fullscreen.lock="fullscreenLoading"></el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-row style="line-height: 65px;">

      <router-link to='add-new'>
      <el-button
          type="primary" round >新增<i class="el-icon-circle-plus-outline el-icon--right"></i>
      </el-button>
      </router-link>
      <el-button style="margin-left: 15px"
          type="primary" round
          @click="openFullScreen1"
          v-loading.fullscreen.lock="fullscreenLoading">刷新<i class="el-icon-refresh el-icon--right"></i>
      </el-button>

    </el-row>
  </div>
</template>

<script>
export default {
  data() {
    return {
      fullscreenLoading: false,
      tableData: []
    }
  },
  methods: {
    openFullScreen1() {
      this.fullscreenLoading = true;
      setTimeout(() => {
        this.fullscreenLoading = false;
        this.loadadminList();
        this.$message({
          message: '刷新成功!!',
          type: 'success'
        });
      }, 1500);
    },
    changeEnable(admin) {
      this.fullscreenLoading = true;
      setTimeout(() => {
        this.fullscreenLoading = false;
        let enableText = ['禁用', '启用'];
        let name;
        if (admin.name != null) {
          name = "名称为:" + admin.name;
        }
        let url = 'http://localhost:9081/admins/' + admin.id;
        if (admin.enable == 1) {
          url += '/enable';
        } else {
          url += '/disable';
        }
        console.log('url = ' + url);
        this.axios
            .create({"headers":{'Authorization':localStorage.getItem("jwt")}})
            .post(url).then((response) => {
          let responseBody = response.data;
          console.log(responseBody);
          if (responseBody.state == 20000) {
            this.$message({
              message: '将【' + name + '】的启用状态设置为【' + enableText[admin.enable] + '】成功!',
              type: 'success'
            });
          } else {
            // this.$message.error(responseBody.message);
            let title = '操作失败';
            this.$alert(responseBody.message, title, {
              confirmButtonText: '确定',
              callback: action => {
                this.loadadminList();
              }
            });
          }
        });
      }, 300);
    },
    handleEdit(admin) {
      this.$router.push({path:'update',query: {id:admin.id}})
    },
    openDeleteConfirn(admin) {

      let message = "此操作将永久删除该项目类别, 是否继续?";
      let title = "提示";
      this.$confirm(message, title, {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }
      ).then(() => {
          this.handleDelete(admin);
        this.fullscreenLoading = true;
        setTimeout(() => {
          this.fullscreenLoading = false;
          this.$message({
            type: 'success',
            message: '删除成功!'
        });
        }, 1000);
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除!'
        });
      });
    },
    handleDelete(admin) {
      console.log("handleDelete.... adminId=" + admin.id);
      let url = "http://localhost:9081/admins/" + admin.id + "/delete";
      console.log("url = " + url);
      this.axios
          .create({"headers":{'Authorization':localStorage.getItem("jwt")}})
          .post(url).then((respose) => {
        let responseBody = respose.data;
        console.log(responseBody);
        if (responseBody.state != 20000) {
          this.$message.error('您的网络出现波动!正在尝试刷新!');
        }
        this.loadadminList();
      });
    },
    loadadminList() {
      console.log('loadadminList ...');
      let url = "http://localhost:9081/admins";
      console.log(url);
      this.axios
          .create({"headers":{'Authorization':localStorage.getItem("jwt")}})
          .get(url).then((respose) => {
        let responseBody = respose.data;
        this.tableData = responseBody.data;
      })
    },
  },

  mounted() {
    this.loadadminList();
  }
}
</script>

<style scoped>

</style>